package com.hoow.shakoow.shake.adapter

import android.annotation.SuppressLint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.hoow.shakoow.R
import com.hoow.shakoow.ai.data.RevObjChatInfoMsg
import com.hoow.shakoow.extend.isValidLink

class ChatInfoAdapter(private val dataList: List<RevObjChatInfoMsg>) :
    RecyclerView.Adapter<ChatInfoAdapter.ViewHolder>() {

    inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) {
        var textAdapterChatName: TextView = view.findViewById(R.id.textAdapterChatName)
        var imageAdapterChatAvatar: ImageView = view.findViewById(R.id.imageAdapterChatAvatar)
        var textAdapterChatMsg: TextView = view.findViewById(R.id.textAdapterChatMsg)
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int,
    ): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.view_adpter_chat_info_msg, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val data: RevObjChatInfoMsg = dataList[position]
        holder.textAdapterChatName.text = data.name + ":"
        holder.textAdapterChatMsg.text = data.msg

        data.headUrl?.let { link ->
            if (link.isValidLink()) {
                Glide.with(holder.imageAdapterChatAvatar)
                    .load(link)
                    .into(holder.imageAdapterChatAvatar)
            }
        }

        if(data.headUrl==null){
            holder.imageAdapterChatAvatar.setImageResource(R.drawable.icon_placeholder)
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }
}